﻿var paginationTotal = 20;//记录总数
var isSearch = false;
function submitsearch() {
    isSearch = true;
    paginationCurrentPage = 1;
    initData(0);
}

function loadData(page, pageSize) {
    var vForm = "";
    if (isSearch == true && isSearch != undefined) {
        vForm = $("#sForm").serializeArray();
    }
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/plan/rate/list/page/" + page + "/pageSize/" + pageSize,
        data: vForm,
        success: function(json) {
            var list = "";
            paginationTotal = json.count;
            if (json.ask == 0) {
                list = "<td colspan='10' class=\"center\">&nbsp;<{$lang.no_data}></td>";
            } else {
               // var i = page == 1 ? 1 : pageSize * (page - 1) + 1;
                $.each(json.result, function(key, val) {
                    if ((key + 1) % 2 == 1) {
                        list += "<tr>";
                    } else {
                        list += "<tr class='even-tr'>";
                    }
                    list += "<td>" + val.pr_id + "</td>";
					list += "<td>" + val.wh_id + '--' +val.wh_name + "</td>";
                    list += "<td>" + val.type + "</td>";
                    list += "<td>" + val.price + "</td>";
                    list += "<td>" + val.discount + "</td>";
                    list += "<td>" + val.currency_code + "</td>";
                    list += "<td>" + val.range_start + "</td>";
                    list += "<td>" + val.range_end + "</td>";
                    list += "<td>" + val.order + "</td>";
                    list += "<td class=\"center\"><a href=\"javascript:edit(" + val.pr_id + ")\"><img src=\"/images/icon/edit.gif\" title=\"<{$lang.edit}>\"></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"javascript:delelteInfo(" + val.pr_id + ")\"><img src=\"/images/icon/delete.gif\" title=\"<{$lang.delete}>\"></a></td>";
                    list += "</tr>";
                });
            }
            $("#loadListData").html(list);
        }
    });
}

//<{$lang.delete}>
function delelteInfo(id) {
    if (id == '' || id == undefined) {
        return false;
    }
    Uid = id;
    $('#right_confirm').dialog('open');
}

function dialogclose() {
    $("#dialog").dialog("close");
}

$(function() {
    initData(0);
    $("#dialog").dialog({
        autoOpen:false,
        modal:true,
        width:380,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                create();
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
            $('#createForm')[0].reset();
            $('#pr_id').val('');//清除隐藏表单
        }
    });

    $("#dialog-tabs").dialog({
        autoOpen:false,
        modal:true,
        width:680,
        show:"slide"
    });

    //<{$lang.delete}>
    $("#right_confirm").dialog({
        autoOpen:false,
        modal:true,
        height:160,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                $.ajax({
                    type: "post",
                    async: false,
                    dataType: "json",
                    url: "/plan/rate/delete",
                    data: "pr_id=" + Uid,
                    success: function(json) {
                        if (json == null) {
                            return;
                        }
                        if (json.ask == '1') {
                            paginationTotal = paginationTotal - 1;
                            initData(paginationCurrentPage - 1);
                            messags = "<{$lang.success}><{$lang.delete}>！";
                        } else {
                            messags = "<{$lang.delete}><{$lang.fail}>！";
                        }
                        alertTip(messags);
                    }
                });
                $(this).dialog('close');
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        }

    });

    //<{$lang.add}>
    $("#create").click(function() {
        tip('');
          $("#dialog").dialog({title:'<{$lang.add}>套餐',autoOpen:true});
    });
    $("#close").click(function() {
        dialogclose();
    });
});

function create() {
    vForm = "";
    vForm = $("#createForm").serializeArray();
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/plan/rate/edit",
        data: vForm,
        success: function(json) {
            if (json == null)return;
            if (json.ask != '0') {
                if (json.ask == '1') {
                    initData(0);
                } else {
                    loadData(paginationCurrentPage, paginationPageSize);
                }
                alertTip(json.msg);
                dialogclose();
            } else {
                if (json.error == null)return;
                var html = '';
                $.each(json.error, function(key, val) {
                    html += '<span><img src="/images/no.gif">' + val + '</span>';
                })
                tip(html);
            }
        }
    });
}
//<{$lang.edit}>套餐
function edit(id) {
    tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/plan/rate/info",
        data: "pr_id=" + id,
        success: function(json) {
            if (json == null) {
                return;
            }
            if (json.ask == 1) {
                $("[name=pr_id]").val(json.result.pr_id);
				$("[name=wh_id]").val(json.result.wh_id);
                $("[name=type]").val(json.result.type);
                $("[name=order]").val(json.result.order);
                $("[name=range_start]").val(json.result.range_start);
                $("[name=range_end]").val(json.result.range_end);
                $("[name=discount]").val(json.result.discount);
                $("[name=price]").val(json.result.price);
                $("[name=currency_code]").val(json.result.currency_code);
                $("#dialog").dialog({title:'<{$lang.edit}>套餐',autoOpen:true});
            }
        }
    });
}



function tip(mess) {
    var fid = $("#tips");
    fid.html(mess);
    if (mess != '') {
        fid.show();
    } else {
        fid.hide();
    }
}
//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
                $(this).dialog("close");
            }
        }
    });
}